//
//  AdapterViewController.swift
//  TXiOSDemoApp
//
//  Created by xicheng on 16/11/21.
//  Copyright © 2016年 xicheng. All rights reserved.
//

import UIKit

class AdapterViewController: TXBaseViewController {

  fileprivate var tableView:UITableView! = nil
  
  fileprivate var adapter:DemoListAdapter! = nil
  
  override func initView() {
    self.naviBar.title = "AdapterViewController"
    
    self.tableView = UITableView(frame: self.view.bounds)
    self.tableView.backgroundColor = UIColor.white
    self.tableView.contentInset = UIEdgeInsetsMake(kNaviHeight, 0, 0, 0)
    self.view.addSubview(self.tableView)
    self.adapter = DemoListAdapter(vc: self)
    self.adapter.setupTableView(tableView: self.tableView)
  }
  
  override func initListener() {
    
    self.adapter.refreshListener = {
      [unowned self] in
      self.getDemoList(refreshFlag: true)
    }
    
    self.adapter.loadNextListener = {
      [unowned self] (pageIndex,pageSize) in
      API.demoList(WithPageIndex: "\(pageIndex)", AndPageSize: "\(pageSize)", AndError: { (demoPageListServerModel, error) in
        
        }, AndOnFinish: { [weak self](task) -> Bool in
          guard let _ = self else {
            return false
          }
          return true
      }) { [weak self] ( demoPageListServerModel) in
        
        guard let instance = self else {
          return
        }
        instance.adapter.addLoadData(baseListModel: demoPageListServerModel.data)
        
      }
    }
  }
  
  
  override func initData() {
   
    
    self.getDemoList(refreshFlag: false)
  }
  
  
  private func getDemoList(refreshFlag:Bool){
  
    API.demoList(WithPageIndex: "1", AndPageSize: "10", AndError: { (demoPageListServerModel, error) in
      TXLogError("error:\(error)")
      }, AndOnFinish: { [weak self](task) -> Bool in
        guard let _ = self else {
          return false
        }
        return true
      }) { [weak self] ( demoPageListServerModel) in
        
        guard let instance = self else {
          return
        }
        instance.adapter.refreshCount(baseListModel: demoPageListServerModel.data)
        instance.toMain()
        if refreshFlag {
          instance.tableView.tx_endRefresh()
        }

    }
    
  }

}



fileprivate class DemoListAdapter:BaseListTableViewAdapter<DemoModel,DemoListCell,AdapterViewController>{

  fileprivate override func setupCell(_ tableView: UITableView, cell: DemoListCell, model: DemoModel, indexPath: IndexPath) {
    cell.demoModel = model
  }
  
  fileprivate override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    guard let model = self.dataList?[indexPath.row] else {
      return 0
    }
    
    let cellHeight = DemoListCell.calculateHeight(WithMode: model)
    return cellHeight
  }
  
  
  fileprivate override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
  }
  
  fileprivate override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
    return true
  }
  
  
}
